package com.demo;

import java.util.HashMap;
/**
 * @author XiaHui
 * @date 2017年6月10日 下午2:35:35
 */
import java.util.List;
import java.util.Map;

import com.sun.javafx.webkit.Accessor;
import com.sun.webkit.WebPage;
import com.sun.webkit.event.WCKeyEvent;
import com.sun.webkit.event.WCMouseEvent;

import javafx.application.Application;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventDispatchChain;
import javafx.event.EventDispatcher;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewBrowser extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {
		Pane root = new WebViewPane();
		primaryStage.setScene(new Scene(root, 1024, 768));
		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
	 private static final Map<Object, Integer> idMap = new HashMap<Object, Integer>();
	/**
	 * Create a resizable WebView pane
	 */
	 
	 static {
	        idMap.put(MouseButton.NONE, WCMouseEvent.NOBUTTON);
	        idMap.put(MouseButton.PRIMARY, WCMouseEvent.BUTTON1);
	        idMap.put(MouseButton.MIDDLE, WCMouseEvent.BUTTON2);
	        idMap.put(MouseButton.SECONDARY, WCMouseEvent.BUTTON3);

	        idMap.put(MouseEvent.MOUSE_PRESSED, WCMouseEvent.MOUSE_PRESSED);
	        idMap.put(MouseEvent.MOUSE_RELEASED, WCMouseEvent.MOUSE_RELEASED);
	        idMap.put(MouseEvent.MOUSE_MOVED, WCMouseEvent.MOUSE_MOVED);
	        idMap.put(MouseEvent.MOUSE_DRAGGED, WCMouseEvent.MOUSE_DRAGGED);

	        idMap.put(KeyEvent.KEY_PRESSED, WCKeyEvent.KEY_PRESSED);
	        idMap.put(KeyEvent.KEY_RELEASED, WCKeyEvent.KEY_RELEASED);
	        idMap.put(KeyEvent.KEY_TYPED, WCKeyEvent.KEY_TYPED);
	    }
	WebPage webPage ;
	public class WebViewPane extends Pane {

		public WebViewPane() {
			VBox.setVgrow(this, Priority.ALWAYS);
			setMaxWidth(Double.MAX_VALUE);
			setMaxHeight(Double.MAX_VALUE);

			TextField locationField = new TextField("http://www.baidu.com");
			Button goButton = new Button("Go");
			
			WebView webView = new WebView();
			WebEngine webEngine = webView.getEngine();
			 webPage = Accessor.getPageFor(webEngine);
			webPage.setJavaScriptEnabled(true);
			
			
			
			webView.setMinSize(500, 400);
			webView.setPrefSize(500, 400);
			
			webEngine.load("http://www.baidu.com");

			webPage.setEditable(true);

            EventDispatcher eventDispatcher=new EventDispatcher() {

				@Override
				public Event dispatchEvent(Event event, EventDispatchChain tail) {
					//tail.dispatchEvent(event);
					if(event.getEventType()==MouseEvent.ANY) {
						
					}
					return event;
				}
            	
            };
           
          
			webView.setEventDispatcher(eventDispatcher);
			
		
			//webView.addEventHandler(eventType, eventHandler);
			//webView.buildEventDispatchChain(tail)
			
			EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
				@Override
				public void handle(ActionEvent event) {
					webEngine.load(locationField.getText().startsWith("http://") ? locationField.getText() : "http://" + locationField.getText());
				}
			};

			goButton.setDefaultButton(true);
			goButton.setOnAction(goAction);

			locationField.setMaxHeight(Double.MAX_VALUE);
			locationField.setOnAction(goAction);
			
			webEngine.locationProperty().addListener(new ChangeListener<String>() {
				@Override
				public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
					locationField.setText(newValue);
				}
			});
			
			
			
			
			
			GridPane grid = new GridPane();
			grid.setVgap(5);
			grid.setHgap(5);
			GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
			GridPane.setConstraints(goButton, 1, 0);
			GridPane.setConstraints(webView, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
			grid.getColumnConstraints().addAll(
					new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true),
					new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true));
			grid.getChildren().addAll(locationField, goButton, webView);
			getChildren().add(grid);
		}
		private void processMouseEvent(MouseEvent ev) {
	        if (webPage == null) {
	            return;
	        }

	        // RT-24511
	        EventType<? extends MouseEvent> type = ev.getEventType();
	        double x = ev.getX();
	        double y = ev.getY();
	        double screenX = ev.getScreenX();
	        double screenY = ev.getScreenY();
	        if (type == MouseEvent.MOUSE_EXITED) {
	            type = MouseEvent.MOUSE_MOVED;
	            x = Short.MIN_VALUE;
	            y = Short.MIN_VALUE;
	            Point2D screenPoint = localToScreen(x, y);
	            if (screenPoint == null) {
	                return;
	            }
	            screenX = screenPoint.getX();
	            screenY = screenPoint.getY();
	        }

	        final Integer id = idMap.get(type);
	        if (id == null) {
	            // not supported by webkit
	            return;
	        }
	        WCMouseEvent mouseEvent =
	                new WCMouseEvent(id, idMap.get(ev.getButton()),
	                    ev.getClickCount(), (int) x, (int) y,
	                    (int) screenX, (int) screenY,
	                    System.currentTimeMillis(),
	                    ev.isShiftDown(), ev.isControlDown(), ev.isAltDown(),
	                    ev.isMetaDown(), ev.isPopupTrigger());
	        webPage.dispatchMouseEvent(mouseEvent);
	        ev.consume();
	    }
		@Override
		protected void layoutChildren() {
			List<Node> managed = getManagedChildren();
			double width = getWidth();
			double height = getHeight();
			double top = getInsets().getTop();
			double right = getInsets().getRight();
			double left = getInsets().getLeft();
			double bottom = getInsets().getBottom();
			for (int i = 0; i < managed.size(); i++) {
				Node child = managed.get(i);
				layoutInArea(child, left, top,width - left - right, height - top - bottom,0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
			}
		}
	}
}